﻿using System;
using System.Text;

namespace SocketUtil.Server
{
    public static class Extensions
    {
        public static string ToStringEx(this byte[] buff, int len)
        {
            if (buff == null)
            {
                return $"[(null)]({len})";
            }
            var builder = new StringBuilder();
            builder.AppendLine($"---{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff")}---");
            builder.Append("[ ");
            for (int i = 0; i < buff.Length; i++)
            {
                builder.Append(buff[i].ToString("x2"));
                builder.Append(buff[i].ToString(" "));
                if (i == len - 1)
                {
                    builder.Append(buff[i].ToString(" | "));
                }
            }
            builder.Append($"]({len})");
            builder.AppendLine();
            var str = Encoding.UTF8.GetString(buff, 0, len);
            builder.AppendLine($"[{str}]({len})");
            builder.AppendLine($"------");
            return builder.ToString();
        }
    }
}